package AddonsSheepContoller.Utils;

import org.bukkit.Bukkit;
import org.bukkit.GameRule;
import org.bukkit.World;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

public class CommandExecutor {
    public static void runCommand(CommandSender sender, String command, String[] args)
    {
        if(sender == null) sender = Bukkit.getConsoleSender();
        StringBuilder executeCommand = new StringBuilder(); executeCommand.append(command);
        if(args.length != 0) for(String argsFormat : args) { executeCommand.append(" ").append(argsFormat); }
        Bukkit.dispatchCommand(sender, executeCommand.toString());
    }

    public static void runCommandQuiet(CommandSender sender, String command, String[] args)
    {
        if(sender == null) sender = Bukkit.getConsoleSender();
        StringBuilder executeCommand = new StringBuilder(); executeCommand.append(command);
        if(args.length != 0)  for(String argsFormat : args) { executeCommand.append(" ").append(argsFormat); }
        for(World format : Bukkit.getWorlds()) format.setGameRule(GameRule.SEND_COMMAND_FEEDBACK, true);
        Bukkit.dispatchCommand(sender, executeCommand.toString());
        for(World format : Bukkit.getWorlds()) format.setGameRule(GameRule.SEND_COMMAND_FEEDBACK, false);
    }

    public static void senderActionbarTitle(Player target, String contents)
    { runCommand(null, String.format("title %s actionbar \"%s\"", target.getName(), contents), new String[]{}); }
}
